import router from '@/router'
import store from '@/store'
import {
  Message
} from 'element-ui'
const TOKEN_BAIMINGDANG = ['/login', '/404']
router.beforeEach(async (to, from, next) => {
  const token = store.getters.token
  if (token) {
    // 得到用户信息
    const userInfo = store.getters.userInfo
    // 判断是否存在
    if (!userInfo.username) {
      await store.dispatch('user/userInfo')
      // 如果用户信息不存在 调用接口得到数据
      const userInfo = await store.dispatch('user/userInfo')
      // 动态获取当前用户的权限路由
      // const myroutes = await store.dispatch('permission/set_routes', userInfo.data.roles.menus)
    }
    // router.addRoutes([
    //   ...myroutes,
    //   {
    //     path: '*',
    //     redirect: '/404',
    //     hidden: true
    //   }
    // ])
    // next(to.path)
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (TOKEN_BAIMINGDANG.indexOf(to.path) != -1) {
      next()
    } else {
      next(`/login?_redirect=${to.path}`)
    }
  }
})
